/**
 * Created by JetBrains WebStorm.
 * User: Liucw
 * Date: 12-8-29
 * Time: 下午8:43
 * To change this template use File | Settings | File Templates.
 */
Vector2 = function(x,y){
    this.x = x||0;
    this.y = y||0;
};

Vector2.prototype = {
    constructor:Vector2,
    multiplyScalar:function(s){
        this.x *= s;
        this.y *= s;
        return this;
    },
    divideScalar:function(s){
        if(s){
            this.x /= s;
            this.y /= s;
        }
        else{
            return this.set(0,0);
        }
        return this;
    },
    dot:function(v){
        return this.x*v.x + this.y*v.y;
    },
    lengthSq:function(){
        return this.x*this.x + this.y*this.y;
    },
    length:function(){
        return Math.sqrt(this.lengthSq());
    },
    normalize:function(){
        return this.divideScalar(this.length());
    },
    //把向量v当作是法向量，折射过去
    reflectionSelf:function(v){
        var nv = v.normalize();
        this.subSelf(nv.multiplyScalar(2*this.dot(nv)));
    },
    //减去向量v
    subSelf:function(v){
        this.x -= v.x;
        this.y -= v.y;
        return this;
    },
    //加上向量v
    addSelf:function(v){
        this.x += v.x;
        this.y += v.y;
        return this;
    },
    printSelf:function(){
        console.log("Vector.x:" + this.x + "|Vector.y:" + this.y);
    },
    //垂直向量
    vertical:function(){
        return new Vector2(-this.y,this.x);
    },
    //点的方法
    //点到由p1、p2确定的直线上的距离
    distanceToLine:function(p1,p2){
        if(p2.x === p1.x){
            return Math.abs(this.y - p1.y);
        }
        else if(p2.y === p1.y){
            return Math.abs(this.x - p1.x);
        }
        else{
            var A = (p2.y - p1.y)/(p2.x - p1.x);
            var B = -1;
            var C = p1.y - A*p1.x;
            return Math.abs(A*this.x + B*this.y +C)/Math.sqrt(A*A + B*B);
        }
    },
    /**
     * 在Y轴向下的坐标系中，是顺时针旋转
     * @param p
     * @param theta
     */
    rotateSelf: function (p, theta) {
        var v = Vector2.sub(this, p);
        theta *= Math.PI / 180;
        var R = [[Math.cos(theta), -Math.sin(theta)], [Math.sin(theta), Math.cos(theta)]];
        this.x = p.x + R[0][0] * v.x + R[0][1] * v.y;
        this.y = p.y + R[1][0] * v.x + R[1][1] * v.y;
    },
    /**
     * 在Y轴向下的坐标系中，是逆时针旋转
     * @param p
     * @param theta
     */
    rotateSelfReverse: function (p, theta) {
        var v = Vector2.sub(this, p);
        theta *= Math.PI / 180;
        var R = [[Math.cos(theta), Math.sin(theta)], [-Math.sin(theta), Math.cos(theta)]];
        this.x = p.x + R[0][0] * v.x + R[0][1] * v.y;
        this.y = p.y + R[1][0] * v.x + R[1][1] * v.y;
    }
};

Vector2.sub = function(v1,v2){
    return new Vector2(v1.x - v2.x,v1.y - v2.y);
};

Vector2.rotate = function(v1,v2,theta){
    var v = new Vector2();
    v.sub(v1,v2);
    theta *= Math.PI/180;
    var R = [[Math.cos(theta),-Math.sin(theta)],[Math.sin(theta),Math.cos(theta)]];
    return new Vector2(v2.x + R[0,0]*v.x + R[0,1]*v.y,v2.y + R[1,0]*v.x + R[1,1]*v.y);
}



